import { container, singleton } from "tsyringe";
import { RobotAppPage } from "./page";
import { RobotApp } from "./app";
import { getConfig } from "../tokens";
import { FloatyWindow } from "../autox/floatWindow";

@singleton()
export class Context {
    activityStock: string[] = [];
    packageStock: string[] = [];

    currentPackage: string;
    currentActivity: string;
    currentApp: RobotApp;
    currentPage: RobotAppPage;

    private floatWindow: FloatyWindow;
    constructor() {
        this.floatWindow = container.resolve(FloatyWindow)
    }

    setTip(title: string) {
        this.updateUi({
            tip: title
        })
    }

    setCurrentActivity(activity: string) {
        if (this.currentActivity !== activity) {
            this.activityStock.push(activity)
            this.currentActivity = activity;
        }
        getConfig().set(`LATEST_ACTIVITY`, activity)
    }

    getLatestPackage() {
        return getConfig().get<string>(`LATEST_PACKAGE`)
    }
    getLatestActivity() {
        return getConfig().get<string>(`LATEST_ACTIVITY`)
    }

    setCurrentPackage(pkg: string) {
        if (this.currentPackage !== pkg) {
            this.packageStock.push(pkg);
            this.currentPackage = pkg;
        }
        getConfig().set(`LATEST_PACKAGE`, pkg)
    }

    setCurrentApp(app: RobotApp) {
        if (this.currentApp !== app) {
            this.currentApp?.close()
            this.currentApp = app;
            this.currentApp?.open()
        }
        this.updateUi({
            currentApp: this.currentApp?.title
        })
    }

    setCurrentPage(page?: RobotAppPage, latestPackage?: string) {
        if (this.currentPage !== page) {
            this.currentPage?.out()
            this.currentPage = page;
            this.currentPage?.in(latestPackage)
        }
        this.updateUi({ currentAppPage: this.currentPage?.title })
    }

    updateUi(data: { currentApp?: string, currentAppPage?: string, currentTask?: string, currentRoom?: string, currentStatus?: string, currentResult?: string, kwaiCurrentMoney?: string, kwaiTodayCount?: string, kwaiTodayMoney?: string, nebulaCurrentMoney?: string, nebulaTodayCount?: string, nebulaTodayMoney?: string, tip?: string, toast?: string }) {
        this.floatWindow.emit('UPDATE_FLOAT_WINDOW_STATE_CHANGE', data)
    }
}
